package com.tairanchina.md.meeting.resource;

import com.alibaba.fastjson.JSON;

import com.tairanchina.md.elife.rabbit.client.consumer.RabbitConsumerClient;
import com.tairanchina.md.elife.rabbit.constants.ConnConstants;
import com.tairanchina.md.elife.rabbit.task.RabbitMQTask;
import com.tairanchina.md.meeting.constants.MeetingConstants;
import com.tairanchina.md.meeting.constants.MeetingProvider;
import com.tairanchina.md.meeting.dto.ProgramTipDto;

import com.tairanchina.md.mallorder.order.dto.YearPartyTipDto;
import com.tairanchina.md.meeting.entity.YearMeetingProgram;

import com.tairanchina.md.meeting.entity.YearMeetingUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;


/**
 * 向页面推送用户签到信息
 */

@ServerEndpoint("/program/tip")
public class PushProgramTipWS implements RabbitMQTask,Runnable
{
    Logger logger = LoggerFactory.getLogger(PushProgramTipWS.class);


    private Session session;

    private static Set<PushProgramTipWS> tips = new HashSet();


    @OnMessage
    public void onMessage(String message, Session session) throws IOException, InterruptedException
    {


    }

    @OnOpen
    public void onOpen(Session session)
    {
        this.session = session;
        System.out.println("客户端已连接------------");
        tips.add(this);
    }

    @OnClose
    public void onClose()
    {
        System.out.println("客户端连接断开------------");

        tips.remove(this);
    }

    @OnError
    public void onError(Session session, Throwable error)
    {
        System.out.println("Connection error" + error);
        tips.remove(this);
    }

    @Override
    public void consumerMsg(Object o,Object... objects) throws Exception {

            YearPartyTipDto tipMsg = (YearPartyTipDto)o;

            if(tipMsg.getTipTime() >= MeetingConstants.MEETING_STARTTIME && tipMsg.getTipTime() <= MeetingConstants.MEETING_ENDTIME){

                //获取打赏节目信息
                YearMeetingProgram program = MeetingProvider.programService.findProgramById(tipMsg.getProgramId());

                //获取打赏用户信息
                YearMeetingUser tipUser = MeetingProvider.userService.findUserByPhone(tipMsg.getUserPhone());
                if(program !=null && tipUser != null ){

                    //更新节目的打赏信息
                    program.setTipMoney(program.getTipMoney()+tipMsg.getTipMoney());
                    MeetingProvider.programService.updateProgram(program);
                    for (PushProgramTipWS tipWS : tips){
                        sentText(program,tipUser,tipMsg,tipWS.session);
                    }

                }
            }
    }


    //发送弹幕
    private void sentText(YearMeetingProgram program,YearMeetingUser tipUser ,YearPartyTipDto tipMsg,Session session){

        try {

        //进行打赏弹幕
        ProgramTipDto programTipDto = new ProgramTipDto();
        programTipDto.setTipMoney(tipMsg.getTipMoney());
        programTipDto.setDepartment(tipUser.getDepartment());
        programTipDto.setProgramName(program.getProgramName());
        programTipDto.setUserName(tipUser.getUserName());
        programTipDto.setUserPic(tipUser.getUserPic());
        programTipDto.setCompany(tipUser.getCompany());
        session.getBasicRemote().sendText(JSON.toJSONString(programTipDto));

        } catch (IOException e) {
            e.printStackTrace();
            logger.error("fas失败,e:{}",e);
        }
    }

    @Override
    public void run() {
        while(true){
            RabbitConsumerClient consumerClient = ConnConstants.getConsumerClient();

            try {
                consumerClient.receiveMessage(MeetingConstants.QUEUE_NAME_YEAR_REWARD,this);
            } catch (Exception e) {
                e.printStackTrace();
                logger.error("节目打赏消息消费失败,e:{}",e);
            }

        }
    }
}
